package KK.Time
{
	import flash.utils.getTimer;
	
	import mx.core.Singleton;

	public class FrameWheel
	{
		
		static public const START:uint=0x0;
		static public const STOP:uint=0x1;
		static private const HIGH_FRAME_RATE:int=1000/60;//高帧
		static private const MID_FRAME_RATE:int=1000/24;//中帧
		static private const LOW_FRAME_RATE:int=1000/12;//低帧
		
		static private const RATE:int=60;
		
		static private var tick:uint; 
		
		
		static private function initTimerWheel():Vector.<Vector.<IWheel>>{
			var result:Vector.<Vector.<IWheel>>=new Vector.<Vector.<IWheel>>(RATE,true);
			var i:int=0;
			while(i<RATE){
				
				result[i]=new Vector.<IWheel>();
				i++;
			} 
			return result;
		}

		static private var TimerWheel:Vector.<Vector.<IWheel>>=initTimerWheel();
		
		
		
		static private var _instance:FrameWheel;
		
		public function FrameWheel(sinleton:Singleton):void{
			if(_instance){
				throw new Error("this singleton class , pless using get Instance get instance");
			}
			_instance=this;
			init();
		}
		
		private function init():void{
			initTimer();
		}
		
		
		
		
		private function Remove(clip:IWheel):void{
			var rect:Vector.<int>=clip.getWheelArea();
			var i:int=0;
			const len:int=rect.length;
			while(i<len){
				FindRemove(rect[i],clip);
				i++;
			}
			
		}
		
		
		
		
		/**
		 * 初始化定时器  
		 * 
		 */
		private function initTimer():void{
			Time.setTimer(Timeout,HIGH_FRAME_RATE);
		}
		/**
		 * 超时  
		 * 
		 */
		private function Timeout():void{
			var Clip:IWheel;
			tick++;
			if(tick>=RATE){
				tick=0;
			}
			
			for each(Clip in TimerWheel[tick]){
				Clip.frameState&&Clip.UpdateFrame();
			}
		}
		
		/**
		 * 查找并移除 
		 * @param id
		 * @param clip
		 * 
		 */
		private function FindRemove(id:int,clip:IWheel):void{
			var i:int=0;
			var len:int=TimerWheel[id].length;
			for(i=0;
				i<len;
				i++){

				if(TimerWheel[id][i]==clip){
					TimerWheel[id].splice(i,1);
					break;
				}
			}
		}
		
		
		/**
		 * 添加  
		 * @param Clip
		 * 
		 */
		
		static public var currID:int=0;
		private function Add(Clip:IWheel):void{
			
			if(Clip.frameRate>0&&Clip.frameRate<=RATE){

				
				var _wheelArea:Vector.<int>=new Vector.<int>(Clip.frameRate,true);
				const SETP:Number=RATE/Clip.frameRate;
				var i:Number;
				var j:int;
				for( i=0,j=0;
					i<RATE;
					i+=SETP,j++)
				{
					TimerWheel[Math.ceil(i)].push(Clip);
					_wheelArea[j]=Math.ceil(i);
				}
				
				Clip.setWheelArea(_wheelArea);
			}else{
				LogError("添加帧频超出范围");
			}
		}
		
		/**
		 * 停止  
		 * 
		 */
		private function stop(clip:IWheel):void{
//			clip.
		}
		
		
		/**
		 *停止循环  
		 * @param clip
		 * 
		 */
		static public function StopWheel(clip:IWheel):void{
			Instance.stop(clip);
		}
		
		/**
		 * 添加时间轮 
		 * @param frameObj
		 * 
		 */
		static public function AddWheel(clip:IWheel):void{
			Instance.Add(clip);
		}
		
		/**
		 * 移除wheel 
		 * @param Clip
		 * 
		 */
		static public function RemoveWheel(clip:IWheel):void{
			Instance.Remove(clip);
			
		}
		
		/**
		 * 获取单例  
		 * @return 
		 * 
		 */
		public static function get Instance ():FrameWheel{
			if(!_instance){
				new FrameWheel(new Singleton());
				
			}
			
			return _instance;
		}
	}
}

class Singleton{
	
}